﻿#include "GameView.h"
#include "../lib/src/basic/minidb.h"
#include "../lib/src/game/MiscDefine.h"

#include <stdio.h>
#include <stdlib.h>

#pragma comment(lib, "../Debug/ComLib.lib")
#pragma warning(disable:4996)

int  LoadMemImage(void* pMemData, long len, CImage& img) {
	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, len);
	void* pData = GlobalLock(hGlobal);
	memcpy(pData, pMemData, len);
	GlobalUnlock(hGlobal);
	IStream* pStream = NULL;
	if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) != S_OK) {
		return -1;
	}
	if (!SUCCEEDED(img.Load(pStream))) {
		pStream->Release();
		GlobalFree(hGlobal);
		return -1;
	}
	pStream->Release();
	GlobalFree(hGlobal);
	return 0;
}

int load_img(int idx, CImage& img) {
	char* buf = 0;
	int len = 0;
	minidb_read(0, idx, &buf, &len);
	LoadMemImage(buf, len, img);
	free(buf);
	return 0;
}

int load_img_all(const char* mini_db_img_path, CImage* mImage) {
	mini_init(mini_db_img_path);
	load_img(0, mImage[0]);		//左上角
	load_img(1, mImage[1]);		//右上角
	load_img(2, mImage[2]);		//左下角
	load_img(3, mImage[3]);		//右下角
	load_img(4, mImage[4]);		//上
	load_img(5, mImage[5]);		//下
	load_img(6, mImage[6]);		//左
	load_img(7, mImage[7]);		//右
	load_img(8, mImage[8]);		//关常
	load_img(9, mImage[9]);		//关悬
	load_img(10, mImage[10]);	//关悬
	load_img(11, mImage[11]);	//方块
	load_img(12, mImage[12]);	//菱形
	return 0;
}

CGameView::CGameView() : mbDrawVerticalLine(false) {
	load_img_all("img_v1.01.bin", mImage);
	mCloseButtonState = 0;
	mMemDC = NULL;
}

void CGameView::initDC() {
	mScreenDC = GetDC(mHwnd);
}

CGameView::~CGameView(void) {
	//ReleaseDC(mHwnd, mScreenDC);
	//SelectObject(memdc, oldb);
	//DeleteObject(membmp);
	//DeleteDC(mMemDC);
}

//state 0:正常 1:盘旋 2:按下
void CGameView::drawCloseButton(HDC* pMemDC) {
	RECT winRect;
	GetClientRect(mHwnd, &winRect);
	switch(mCloseButtonState) {
	case 0:
		//源宽21 高12
		mImage[8].TransparentBlt(*pMemDC, winRect.right - 22 - 15, 2, 21, 12, 0, 0, 21, 12, RGB(255, 0, 255));		//关闭 正常态
		break;
	case 1:
		mImage[9].TransparentBlt(*pMemDC, winRect.right - 22 - 15, 2, 21, 12, 0, 0, 21, 12, RGB(255, 0, 255));		//关闭 盘旋态
		break;
	}
}

void CGameView::drawFrameBackgroundAndBorder(HDC* pMemDC) {
	RECT winRect;
	GetClientRect(mHwnd, &winRect);

	HBRUSH hb = CreateSolidBrush(RGB(255, 0, 255));
	FillRect(*pMemDC, &winRect, hb);	//选用紫色填充整个区域
	DeleteObject(hb);
	RECT magin;
	magin.bottom = winRect.bottom - 5;
	magin.left = winRect.left + 5;
	magin.right = winRect.right - 5;
	magin.top = winRect.top + 5;
	FillRect(*pMemDC, &magin, (HBRUSH)GetStockObject(BLACK_BRUSH));	//选用空的画刷

	//过滤紫色  cx/cy为绝对宽高
	mImage[0].TransparentBlt(*pMemDC, 0, 0, 193, 138, 0, 0, 193, 138, RGB(255, 0, 255));		//左上
	mImage[1].TransparentBlt(*pMemDC, winRect.right - 195, 0, 195, 161, 0, 0, 195, 161, RGB(255, 0, 255));		//右上
	mImage[2].TransparentBlt(*pMemDC, 0, winRect.bottom - 133, 139, 133, 0, 0, 139, 133, RGB(255, 0, 255));		//左下
	mImage[3].TransparentBlt(*pMemDC, winRect.right - 131, winRect.bottom - 111, 131, 111, 0, 0, 131, 111, RGB(255, 0, 255));		//右下


	//源高21
	mImage[4].TransparentBlt(*pMemDC, 138, 0, winRect.right - 138 - 161, 21, 0, 0, 200, 21, RGB(255, 0, 255));		//上

	//源宽200 高28
	mImage[5].TransparentBlt(*pMemDC, 138, winRect.bottom - 28, winRect.right - 139 - 131 + 2, 28, 0, 0, 200, 28, RGB(255, 255, 0));		//下

	//源宽19 高100
	mImage[6].TransparentBlt(*pMemDC, 0, 138, 19, winRect.bottom - 138 - 133, 0, 0, 19, 100, RGB(255, 0, 255));		//左

	//源宽22 高29
	mImage[7].TransparentBlt(*pMemDC, winRect.right - 22, 161, 22, winRect.bottom - 161 - 111, 0, 0, 22, 29, RGB(255, 0, 255));		//右
}

void CGameView::drawPkgCount(HDC* pMemDc) {
	//::SetBkMode(*pMemDc, TRANSPARENT);
	char dbg[128] = {0};
	SelectObject(*pMemDc, GetStockObject(WHITE_PEN));	
	SelectObject(*pMemDc, GetStockObject(GRAY_BRUSH));
	sprintf_s(dbg, sizeof(dbg), "");
	::TextOut(*pMemDc, 55, 2, dbg, strlen(dbg));
}

void CGameView::reDraw() {
	InvalidateRect(mHwnd, NULL, false);
}
ST_NOTIFY_VIEW_ATOMS_CHANGED* gc = 0;

void CGameView::refreshAtom(ST_NOTIFY_VIEW_ATOMS_CHANGED* c) {
	int al = c->msg.data_len + sizeof(CMsg);
	free(gc);
	gc = (ST_NOTIFY_VIEW_ATOMS_CHANGED*)malloc(al);
	memcpy(gc, c, al);
	//UpdateWindow(mHwnd);
	reDraw();
}

void CGameView::onPaint() {
	RECT winRect;
	HBITMAP membmp, oldb;

	mMemDC = CreateCompatibleDC(GetDC(mHwnd));	//对应DeleteDC  而getDC则对应releaseDC
	GetClientRect(mHwnd, &winRect);
	membmp = (HBITMAP)CreateCompatibleBitmap(mScreenDC, winRect.right - winRect.left, winRect.bottom - winRect.top);
	oldb = (HBITMAP)SelectObject(mMemDC, membmp);
	
	drawFrameBackgroundAndBorder(&mMemDC);
	drawPkgCount(&mMemDC);
	drawCloseButton(&mMemDC);

	SelectObject(mMemDC, GetStockObject(NULL_BRUSH));		//不必要通过调用DeleteObject函数来删除GetStockObject储存对象
	SelectObject(mMemDC, GetStockObject(WHITE_PEN));	
	int  lef = 40, top = 30, hei = 150, wid = 200, magin = 50;

	POINT ptPalyer1[4] = {{lef, top}, {lef + wid, top}, {lef + wid, top + hei}, {lef, top + hei}};
	POINT ptPalyer2[4] = {{lef + wid + magin, top}, {lef + wid + wid + magin, top}, {lef + wid + wid + magin, top + hei}, {lef + wid + magin, top + hei}};
	//第一个pt是所有Matrix的基准点 (40,195) 左上, 右上, 右下, 左下    宽应=24 * 34  参看 Usr::draw
	int matrixWid = ITEM_SIDE_LENGTH_IN_PIX * ITEM_WIDTH_COUNT, matrixHeight = ITEM_SIDE_LENGTH_IN_PIX * ITEM_HEIGHT_COUNT;	//576, 450
	POINT ptMatrix[4] = {{lef - 5, top + hei + 15 - 5}, {lef + matrixWid + 2, top + hei + 15 - 5}, {lef + matrixWid + 2, top + hei + 15 + matrixHeight + 2}, {lef - 5, top + hei + 15 + matrixHeight + 2}};
	Polygon(mMemDC, ptPalyer1, 4);
	Polygon(mMemDC, ptPalyer2, 4);
	Polygon(mMemDC, ptMatrix, 4);

	if (gc) {
		for (int i = 0; i < gc->atoms_count; ++i) {
			CImage* img = 0;
			if (ATOM_SHAPE_NORMAL == (gc->atoms[i].data & 0xF)) {
				img = &mImage[11];
			} else if (ATOM_SHAPE_DIAMOND == (gc->atoms[i].data & 0xF)) {
				img = &mImage[12];
			}
			img->StretchBlt(mMemDC, gc->atoms[i].coord.x, gc->atoms[i].coord.y, ITEM_SIDE_LENGTH_IN_PIX, ITEM_SIDE_LENGTH_IN_PIX, 0, 0, 48, 48, SRCCOPY); //atom wid,height:18pix, src-img wid,height:48 pix
		}
	}

	BitBlt(mScreenDC, 0, 0, winRect.right - winRect.left, winRect.bottom - winRect.top, mMemDC, 0, 0, SRCCOPY);
	SelectObject(mMemDC, oldb);
	DeleteObject(membmp);
	DeleteDC(mMemDC);
}